<template>
  <static-content-wrapper
    :designer="designer"
    :settings="settings"
    :field="field"
    :design-state="designState"
    :hidden="field.options.hidden"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
    :allow-select="allowSelect"
  >
    <div
      ref="fieldEditor"
      class="field-editor table-field-editor"
      :style="{
        width: scaleWidth + 'px',
        height: scaleHeight + 'px'
      }"
    >
      <div class="table-back"></div>
      <div class="table-grid">
        <div
          v-for="(row, rowIndex) in rows"
          :key="rowIndex"
          class="table-grid-row"
          :style="{ height: row * scale + 'px', width: '100%' }"
        >
          <div
            v-for="(col, colIndex) in cols"
            :key="colIndex"
            class="table-grid-cell"
            :style="{
              width: col * scale + 'px',
              height: row * scale + 'px'
            }"
          ></div>
        </div>
      </div>
    </div>
  </static-content-wrapper>
</template>

<script>
import emitter from '@/mixins/emitter'
import fieldMixin from '@/mixins/fieldMixin'

export default {
  name: 'TableWidget',
  componentName: 'TableWidget',
  // 必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    settings: Object,
    scale: {
      type: Number,
      default: 1
    },
    allowSelect: {
      type: Boolean,
      default: true
    },

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: ''
    }
  },
  data() {
    return {
      containerStyle: {}
    }
  },
  computed: {
    scaleWidth() {
      return this.field.options.width * this.scale
    },
    scaleHeight() {
      return this.field.options.height * this.scale
    },
    scaleBorderWidth() {
      return this.field.options.widthLine * this.scale
    },
    rows() {
      return this.field.options.rows || []
    },
    cols() {
      return this.field.options.cols || []
    },
    border() {
      return `${this.scaleBorderWidth}px solid ${this.field.options.color}`
    }
  },
  watch: {},
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    this.registerToRefList()
    this.initEventHandler()

    this.handleOnCreated()
  },

  mounted() {
    this.handleOnMounted()
  },

  beforeUnmount() {
    this.unregisterFromRefList()
  },

  methods: {
    getWidgetName(widget) {
      return widget.type + '-widget'
    }
  }
}
</script>

<style lang="scss" scoped>
.table-field-editor {
  overflow: hidden;

  .table-back {
    width: 100%;
    height: 100%;
    background-color: #000000;
    border: 0px solid #ff0000;
    border-radius: 0px;
    opacity: 0;
  }

  .table-grid {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    .table-grid-row {
      display: table-row;

      &:first-child {
        .table-grid-cell {
          border-top: v-bind('border');
        }
      }

      .table-grid-cell {
        display: table-cell;
        border-bottom: v-bind('border');
        border-right: v-bind('border');

        &:first-child {
          border-left: v-bind('border');
        }
      }
    }
  }
}
</style>
